package kr.hkit.android_bookmgr.parser;

import java.net.URL;
import java.util.ArrayList;

import kr.hkit.android_bookmgr.DTO.BookBoard;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class BoardParser {

	private static ArrayList<BookBoard> data;

	public static ArrayList<BookBoard> getData() {
		data = new ArrayList<BookBoard>();
		BookBoard item=null;
		
		String url = "http://121.181.28.212/BookBoard/book/selectProc.jsp";
		
		try {
			URL urladdr = new URL(url);
			String tag;

			XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
			XmlPullParser parser = parserCreator.newPullParser();
			parser.setInput(urladdr.openStream(), null);

			// 파서타입, sml문서의 시작, 끝, 태그안의 값을 식별
			int parserEvent = parser.getEventType();

			// 찾으려고 하는 pass 값은 하나이기 때문에 end_tag 파서타입일때 조건문은 없음
			while (parserEvent != XmlPullParser.END_DOCUMENT) {
				switch (parserEvent) {
				// xml문서가 시작태그일때
				case XmlPullParser.START_TAG:
					tag = parser.getName();
					
					if (tag.compareTo("board_id") == 0) {
						item = new BookBoard();
						String idsrc = parser.nextText();
						item.board_id = idsrc;
					}
					if (tag.compareTo("board_writer") == 0) {
						String writersrc = parser.nextText();
						item.board_writer = writersrc;
					}
					if (tag.compareTo("board_comment") == 0) {
						String commsrc = parser.nextText();
						item.board_comment = commsrc;
					}
					if (tag.compareTo("board_writedate") == 0) {
						String datesrc = parser.nextText();
						item.board_writedate = datesrc;
						data.add(item);
					}
					
					break;
				}
				parserEvent=parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}

	
}
